package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.ContextHolderUtils;
import com.manage.common.core.utils.PageUtil;
import com.manage.system.api.constants.ResponseEnum;
import com.manage.system.api.entity.SysStorageConfig;
import com.manage.system.api.request.oss.StorageAddOrEditParam;
import com.manage.system.api.request.oss.StorageConfigParam;
import com.manage.system.api.service.ISysStorageConfigService;
import com.manage.system.svc.mapper.SysStorageConfigMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 云存储配置表 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-12-09
 */
@AllArgsConstructor
@Service
@Slf4j
public class SysStorageConfigServiceImpl extends ServiceImpl<SysStorageConfigMapper, SysStorageConfig> implements ISysStorageConfigService {

    private final SysStorageConfigMapper sysStorageConfigMapper;

    @Override
    public JsonResult findListPage(StorageConfigParam storageConfigParam) {
        PageUtil.startPageSort(storageConfigParam.getPageNum(), storageConfigParam.getPageSize(),storageConfigParam.getSortField(),storageConfigParam.getSortOrder());
        List<SysStorageConfig> list = sysStorageConfigMapper.findList(storageConfigParam);
        return JsonResult.success(PageUtil.restPage(list));
    }

    @Override
    public JsonResult addStorageConfig(StorageAddOrEditParam storageAddOrEditParam) {
        SysStorageConfig sysStorageConfig = new SysStorageConfig();
        BeanUtils.copyProperties(storageAddOrEditParam,sysStorageConfig);
        long id = IdWorker.getId(sysStorageConfig);
        sysStorageConfig.setId(id);
        sysStorageConfig.setCreateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysStorageConfigMapper.insert(sysStorageConfig));
    }

    @Override
    public JsonResult changeStatus(Long id, Boolean status) {
        SysStorageConfig sysStorageConfig = sysStorageConfigMapper.selectById(id);
        ResponseEnum.STORAGE_NOT_EXISTS.assertIsEmpty(sysStorageConfig);
        sysStorageConfig.setStatus(status);
        return JsonResult.toJsonResult(sysStorageConfigMapper.updateById(sysStorageConfig));
    }

    @Override
    public JsonResult batchDelStorageConfig(List<Long> storageConfigIds) {
        return JsonResult.toJsonResult(sysStorageConfigMapper.deleteBatchIds(storageConfigIds));
    }

    @Override
    public JsonResult editStorageConfig(StorageAddOrEditParam storageAddOrEditParam) {
        SysStorageConfig sysStorageConfig = new SysStorageConfig();
        BeanUtils.copyProperties(storageAddOrEditParam,sysStorageConfig);
        sysStorageConfig.setUpdateStaff(ContextHolderUtils.getCurrentUser().getUserId());
        return JsonResult.toJsonResult(sysStorageConfigMapper.updateById(sysStorageConfig));
    }

    @Override
    public JsonResult findByConfigCode(String configCode) {
        LambdaQueryWrapper<SysStorageConfig> lambda = new QueryWrapper<SysStorageConfig>().lambda()
                .eq(SysStorageConfig::getConfigCode, configCode);
        return JsonResult.success(sysStorageConfigMapper.selectOne(lambda));
    }
}
